<?php


namespace App\Http\Services\Admin\Account;


use App\Http\Constants\Account\CommonCoinReserveCoinLogConstants;
use App\Http\Services\Admin\CommonService;
use App\Models\CommonCoinReserveCoinLog;
use YiluTech\ComplexSearch\ComplexSearch;

class CommonCoinReserveCoinLogService extends ComplexSearch
{
    public $root = CommonCoinReserveCoinLog::class;

    protected $display = 'simple';
    protected $orderBy = 'id desc';
    protected $username = null;
    protected $memberid = null;
    protected $typeName = null;

    protected $headers = [
        ['value' => 'member_id', 'label' => '会员ID'],
        ['value' => 'nickname', 'label' => '会员昵称'],
        ['value' => 'username', 'label' => '会员手机号'],
        ['value' => 'child_type', 'label' => '预扣类型'],
        ['value' => 'coin', 'label' => '预扣红包'],
        ['value' => 'status', 'label' => '预扣状态'],
        ['value' => 'order_no', 'label' => '订单编号'],
        ['value' => 'remark', 'label' => '备注'],
        ['value' => 'create_time', 'label' => '创建时间'],
        ['value' => 'update_time', 'label' => '修改时间'],
    ];

    protected $conditions = [
        'order_no' => [
            'label' => '订单编号',
            'ctype' => 'input',
            'operator' => 'like',
        ],
        'member_id' => [
            'label' => '会员ID',
            'ctype' => 'input',
            'operator' => '=',
            'custom' => true,
        ],
        'username' => [
            'label' => '会员手机号',
            'ctype' => 'input',
            'operator' => 'like',
            'custom' => true,
        ],
        'type_name' => [
            'label' => '预扣类型',
            'ctype' => 'input',
            'operator' => 'like',
            'custom' => true,
        ],
        'status' => [
            'label' => '预扣状态',
            'ctype' => 'select',
            'operator' => CommonCoinReserveCoinLogConstants::COMMONCOIN_STATUS,
        ],
        'created_at' => [
            'label' => '创建时间',
            'ctype' => 'date-in',
            'format' => 'Y-m-d',
            'display' => true
        ]
    ];

    public function make($request)
    {
        if ($request['action'] !== 'prepare') {
            if (isset($request['params'])) {
                foreach ($request['params'] as $key => $value) {
                    if ($value[0] == "username") {
                        $this->username = $value[2];
                        unset($request['params'][$key]);
                    }
                    if ($value[0] == 'member_id') {
                        $this->memberid = $value[2];
                        unset($request['params'][$key]);
                    }
                    if($value[0] == 'type_name') {
                        $this->typename = $value[2];
                        unset($request['params'][$key]);
                    }

                }
            }
        }
        return parent::make($request);

    }

    public function query()
    {
        $select = ['id', 'child_type', 'coin', 'status', 'order_no', 'create_time', 'update_time', 'remark', 'expected_at', 'coin_id'];
        $query = parent::query();
        $query->select($select);
        $query->with("CommonCoinCoin:id,member_id");
        if (!is_null($this->username)) {
            $userInfo = \SharedCache::memberInfoByPhone($this->username);
            if (!empty($userInfo['coin_id'])) {
                $query->where('coin_id', $userInfo['coin_id']);
            }
        }

        if (!is_null($this->memberid)) {
            $userInfo = \SharedCache::getMember($this->memberid);
            if (!empty($userInfo['coin_id'])) {
                $query->where('coin_id', $userInfo['coin_id']);
            }
        }

        if (!is_null($this->typeName)) {
            $childTypeData = array_column(CommonCoinReserveCoinLogConstants::COMMONCOIN_TYPE,'label','value');
            $childTypeFilterData = array_filter($childTypeData,function ($v){
                if ( strpos($v,$this->typeName) !== false)return $v;
            });
            $inMap = array_keys($childTypeFilterData);
            if (!empty($inMap)) {
                $query->whereIn('child_type', $inMap);
            }

        }

        return $query;

    }

    public function get()
    {
        $result = parent::get();
        if ($this->action !== 'prepare') {
            $result = $result->toArray();
            foreach ($result['data'] as $k => &$v) {
                $member = \SharedCache::getMember($v['common_coin_coin']['member_id']);
                $v['username'] = $member['username'] ?: '';
                $v['nickname'] = $member['nickname'] ?: '';
                $v['member_id'] = $v['common_coin_coin']['member_id'];
                unset($v['common_coin_coin']);
            }
            $result = CommonService::objRegroupArr($result);
        }
        return $result;

    }

}
